//
//  ViewController.swift
//  test1
//
//  Created by lewis on 15/12/16.
//  Copyright © 2015年 lewis. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

    @IBAction func onClick(sender: AnyObject) {
        textField1.text = "hello world!";
    }
    @IBOutlet weak var textField1: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true;
    }

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if (text == "\n") {
            textView.resignFirstResponder();
            return false;
        }

        return true
    }

    override func viewWillAppear(animated:Bool){
        super.viewWillAppear(animated);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:",name:UIKeyboardDidShowNotification,object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:",name:UIKeyboardDidHideNotification,object: nil)

    }
    override func viewWillDisappear(animated:Bool){
        super.viewWillDisappear(animated);
        NSNotificationCenter.defaultCenter().removeObserver(self, name:UIKeyboardDidShowNotification,object: nil);
        NSNotificationCenter.defaultCenter().removeObserver(self, name:UIKeyboardDidHideNotification,object: nil)
    }

    func keyboardDidShow(notification:NSNotification){
        NSLog("键盘打开")
    }
    func keyboardDidHide(notification:NSNotification){
        NSLog("键盘关闭")
    }
}

